package com.wushi.lenovo.asia5b.app_di.goods.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.OrderFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.CountModel;
import com.wushi.lenovo.asia5b.app_model.other.CountType;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;


/**
 * Created by zhangyuncai on 2019/1/25.
 */
public class OrderFragmentPresenter extends BasePresenter<OrderFragmentView> {

    ApiService apiService;

    @Inject
    public OrderFragmentPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void getOrdersCount() {
        if (!UserRepository.isLogin()) {
            return;
        }

        Object[] objects = new Object[1];
        Observable<ResultModel<CountModel>> observable = Observable.intervalRange(0, 5, 1, 1, TimeUnit.SECONDS)
                .flatMap(new Function<Long, ObservableSource<ResultModel<CountModel>>>() {
                    @Override
                    public ObservableSource<ResultModel<CountModel>> apply(Long aLong) throws Exception {
                        CountType type;
                        //todo aLong表示次数
                        if (aLong == 0) {
                            type = CountType.Order_Verification;
                        } else if (aLong == 1) {
                            type = CountType.Order_Payment;
                        } else if (aLong == 2) {
                            type = CountType.Order_Problematic;
                        } else if (aLong == 3) {
                            type = CountType.Order_Delivery;
                        } else {
                            type = CountType.Order_History;
                        }
                        objects[0] = type;
                        return apiService.getCount(type.getCountPath(), UserInfo.getMemberId());
                    }
                });
        baseSubscribe(observable,
                new NetParams(false, false, false),
                countModelResultModel -> {
                    if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                        getView().getOrdersCountResult(countModelResultModel, (CountType) objects[0]);
                    }
                });
    }

    public void getCount(CountType type) {
        if (!UserRepository.isLogin()) {
            return;
        }
        baseSubscribe(apiService.getCount(type.getCountPath(), UserInfo.getMemberId()),
                new NetParams(false, false, false),
                countModelResultModel -> {
                    if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                        getView().getOrdersCountResult(countModelResultModel, type);
                    }
                });
    }
}